package com.zzz.demo;

/**
 * @author ZZZ
 * @version 1.0
 * @date 2019/7/15 0015
 */
public class CountDownLatch {

    private volatile int count;

    public CountDownLatch(int count) {
        this.count = count;
    }

    private void countDown() {
        synchronized (this) {
            if (count < 1) {
                return;
            }
            count--;
            if (count < 1) {
                notifyAll();
            }
        }
    }

    private void await() throws InterruptedException {
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }

        synchronized (this) {
            while (count > 0) {
                await();
            }
        }
    }
}
